package com.laicunba.service.profit.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.laicunba.dao.GenericQueryDao;
import com.laicunba.dao.GenericUpdateDao;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.UserService;
import com.laicunba.service.profit.SuiCunBaoProfitService;
import com.laicunba.service.profit.CunPiaoBaoProfitService.CurrentProfitAndPrincipal;

@Service
public class SuiCunBaoProfitServiceImpl implements SuiCunBaoProfitService {

	@Autowired
	protected GenericQueryDao queryDao;
	@Autowired
	private UserService userService;
	

	@Override
	public MergedProfitResult getMergedProfit(String userId) {
		List<Map<String, Object>> list = queryDao.selectList("SCB_PROFIT_VIEW.getMergedProfit", userId);
		MergedProfitResult result = new MergedProfitResult();
		for (Map<String, Object> map : list) {
			if ("principal".equals((String) map.get("data_name"))) {
				result.setPrincipal(BigDecimal.valueOf((Double) map.get("data_value")));
			} else if ("dayprofit".equals((String) map.get("data_name"))) {
				result.setDayProfit(BigDecimal.valueOf((Double) map.get("data_value")));
			} else if ("total_profit".equals((String) map.get("data_name"))) {
				result.setTotalProfit(BigDecimal.valueOf((Double) map.get("data_value")));
			} else if ("withdraw_profit".equals((String) map.get("data_name"))) {
				result.setWithdrawProfit(BigDecimal.valueOf((Double) map.get("data_value")));
			}
		}
		UserDO userDO=	userService.get(UserDO.class, userId);
		if(userDO.getRemainProfit()!=null){
			if(result.getTotalProfit()==null){
				result.setTotalProfit(BigDecimal.ZERO);
			}
			result.setTotalProfit(result.getTotalProfit().add(userDO.getRemainProfit()));
		}
		
		if(result.getDayProfit()==null){
			result.setDayProfit(BigDecimal.ZERO);
		}
		if(result.getTotalProfit()==null){
			result.setTotalProfit(BigDecimal.ZERO);
		}
		if(result.getPrincipal()==null){
			result.setPrincipal(BigDecimal.ZERO);
		}
		if(result.getWithdrawProfit()==null){
			result.setWithdrawProfit(BigDecimal.ZERO);
		}
		return result;
	}

	@Override
	public CurrentProfitAndPrincipal currentProfitAndPrincipal(String userId) {
		List<Map<String, Object>> list = queryDao.selectList("SCB_PROFIT_VIEW.getSCBCurrentProfitAndPrincipal", userId);
		CurrentProfitAndPrincipal result = new CurrentProfitAndPrincipal();	
		if(list.size()>0&&list.get(0)!=null){
			result.setCurrentProfit(BigDecimal.valueOf((Double) list.get(0).get("currentprofit")));
			result.setCurrentPrincipal((BigDecimal) list.get(0).get("currentprincipal"));
		}else{
			result.setCurrentProfit(BigDecimal.valueOf((Double) 0.0));
			result.setCurrentPrincipal(BigDecimal.valueOf((Double) 0.0));
		}
		return  result;
	}
}
